package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BLiquidFuel;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BLiquidFuelMapper;
import com.isdepci.project.carbon.service.IBLiquidFuelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 液体燃料燃烧碳排放Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BLiquidFuelServiceImpl implements IBLiquidFuelService
{
    @Autowired
    private BLiquidFuelMapper bLiquidFuelMapper;


    @Override
    public AjaxResult calLiquidFuel(Long projectId) {
        // 查询所有BSolidFuel数据
        List<BLiquidFuel> bLiquidFuels =bLiquidFuelMapper.selectBLiquidFuelByProjectId(projectId);
        // 初始化总的碳排放量
        BigDecimal totalEmissions = BigDecimal.ZERO;
        for (BLiquidFuel bLiquidFuel : bLiquidFuels) {
            // 计算EF
            BigDecimal EF = (bLiquidFuel.getHeatCarbonContent()
                    .multiply(bLiquidFuel.getCarbonOxidationRate().divide(new BigDecimal("100")))  // 转换为小数
                    .multiply(new BigDecimal("44"))
                    .divide(new BigDecimal("12"), 6, BigDecimal.ROUND_HALF_UP));
            // 计算AD
            BigDecimal AD = bLiquidFuel.getLowerHeatValue()
                    .multiply(bLiquidFuel.getPurchaseQuantity().subtract(bLiquidFuel.getInventoryLevel()));
            // 计算碳排放量 = EF * AD
            BigDecimal emissions = EF.multiply(AD);
            bLiquidFuel.setCarbonEmissions(emissions);
            bLiquidFuelMapper.updateBLiquidFuel(bLiquidFuel);
            // 累加碳排放量
            totalEmissions = totalEmissions.add(emissions);
        }
        return AjaxResult.success(totalEmissions.setScale(2, RoundingMode.HALF_UP));
    }

    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BLiquidFuel> list =  excel.getLiquidFuelList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bLiquidFuelMapper.insertList(list);
        }
        return AjaxResult.success();
    }


    /**
     * 查询液体燃料燃烧碳排放
     */
    @Override
    public BLiquidFuel selectBLiquidFuelById(Long id)
    {
        return bLiquidFuelMapper.selectBLiquidFuelById(id);
    }

    /**
     * 查询液体燃料燃烧碳排放列表
     */
    @Override
    public List<BLiquidFuel> selectBLiquidFuelList(BLiquidFuel bLiquidFuel)
    {
        return bLiquidFuelMapper.selectBLiquidFuelList(bLiquidFuel);
    }

    /**
     * 新增液体燃料燃烧碳排放
     */
    @Override
    public int insertBLiquidFuel(BLiquidFuel bLiquidFuel)
    {
        return bLiquidFuelMapper.insertBLiquidFuel(bLiquidFuel);
    }

    /**
     * 修改液体燃料燃烧碳排放
     */
    @Override
    public int updateBLiquidFuel(BLiquidFuel bLiquidFuel)
    {
        return bLiquidFuelMapper.updateBLiquidFuel(bLiquidFuel);
    }

    /**
     * 批量删除液体燃料燃烧碳排放
     */
    @Override
    public int deleteBLiquidFuelByIds(Long[] ids)
    {
        return bLiquidFuelMapper.deleteBLiquidFuelByIds(ids);
    }

    /**
     * 删除液体燃料燃烧碳排放信息
     */
    @Override
    public int deleteBLiquidFuelById(Long id)
    {
        return bLiquidFuelMapper.deleteBLiquidFuelById(id);
    }


}
